from fastapi import FastAPI
import uvicorn,os
from starlette.staticfiles import StaticFiles
from dotenv import load_dotenv

# 数据库配置
from tortoise.contrib.fastapi import register_tortoise

#### 自定义文件 #####
from config.db import TORTDISE_ORM

from controller.cmf.index import index
from controller.cmf.article import article
from controller.cmf.login import loginIn
from controller.cmf.user import user

from controller.api.user import userApi
#### 自定义文件END #####

app = FastAPI()


#挂载静态文件
app.mount("/static", StaticFiles(directory="static"),name="static") #前端静态资源
app.mount("/admin", StaticFiles(directory="public/admin"),name="admin") #后台管理系统静态页面

# 数据库配置
register_tortoise(app, config=TORTDISE_ORM,add_exception_handlers=True) #generate_schemas=True 自动生成数据表 #add_exception_handlers调试信息

#后台路由
app.include_router(index, prefix="/cmf/index", tags=["后台首页"])
app.include_router(article, prefix="/cmf/article", tags=["后台文章"])
app.include_router(user, prefix="/cmf/user", tags=["用户管理"])
app.include_router(loginIn, prefix="/cmf/login", tags=["登录"])
#接口路由
app.include_router(userApi, prefix="/api/user", tags=["用户列表"])

load_dotenv()
serverPort = os.getenv("HTTP_SERVER_PORT", 8000)
logLevel  = os.getenv("APP_ENV","dev")
if logLevel == "dev":
    logLevel = "trace"
elif logLevel == "test":
    logLevel = "info"
else:
    logLevel = "warning"
if __name__ == "__main__":
    uvicorn.run("main:app", port=int(serverPort),  reload=False, log_level=logLevel)